﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);
      if(!(Page.IsCallback||Page.IsPostBack))
         read();
         TreeView1.EnableViewState = true;
         //if (TreeView1.SelectedNode!=null)
         //TextBox1.Text = TreeView1.SelectedNode.Text;
    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        XmlNode rssUrl = null;
        string name = ((TreeView)sender).SelectedNode.Text;
        XmlDocument document = new XmlDocument();
        document.Load(@"D:\c#\webproject1\rss3\GhostStories.xml");
        rssUrl = SearchNode(document.DocumentElement, name);
        if (rssUrl!=null&&rssUrl.NextSibling.HasChildNodes)
            Label1.Text = rssUrl.NextSibling.FirstChild.Value;
        //else
        //    TextBox1.Text = "http://autosearch.gd.vnet.cn/";
     
    }
    protected XmlNode SearchNode(XmlNode root, string name)
    {
        XmlNode Url=null;
        XmlNode text=null;
        if (root == null)
            return null;
        if (root is XmlElement)
        {
            if (root.HasChildNodes)
            {
               
                text = SearchNode(root.FirstChild, name);
                if(text!=null)
                    Url=text;
            }
            if (root.NextSibling != null)
            {
                text=SearchNode(root.NextSibling, name);
                if (text != null)
                    Url = text;
            }
        }
        else if(root is XmlText)
        {
             if (root.Value == name)
                {
                    Url = root.ParentNode;
                    return Url;
                }
        }
        if(Url!=null)
            return Url;
        else
            return null;
    }
    protected void read()
    {
        TreeView1.Nodes.Clear();
        DropDownList1.Items.Clear();
        XmlDocument document = new XmlDocument();
        document.Load(@"D:\c#\webproject1\rss3\GhostStories.xml");
        RecurseXmlDocument((XmlNode)document.DocumentElement,TreeView1.Nodes);
        GroupList((XmlNode)document.DocumentElement.FirstChild);

    }
    private void GroupList(XmlNode root)
    {
        DropDownList1.Items.Add(root.Name);
        if (root.NextSibling != null)
            GroupList(root.NextSibling);
    }
    private void RecurseXmlDocument(XmlNode root,TreeNodeCollection treeroot)
    {
        if (root == null)
            return;
        if (root is XmlElement)
        {

            if (root.HasChildNodes)
            {
                
                ListBox1.Items.Add(root.Name);
                if (root.Name == "rss")
                {
                    string text = root.FirstChild.FirstChild.Value;
                    TreeNode t = creatnode(treeroot,text);
                    //RecurseXmlDocument(root.FirstChild, t.ChildNodes);
                }
                else
                {
                    TreeNode t = creatnode(treeroot, root.Name);
                    RecurseXmlDocument(root.FirstChild, t.ChildNodes);
                }
            }
            if (root.NextSibling != null)
                RecurseXmlDocument(root.NextSibling,treeroot);

        }
       
    }
    protected TreeNode creatnode(TreeNodeCollection t, string text)
    {
        TreeNode t2 = new TreeNode();
        t2.Text = text;
        t.Add(t2);
        return t2;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TexBox1.Text == null)
            return;
        System.Net.WebRequest myRequest;
        string Url = null;
        string name = null;
        string Group = DropDownList1.SelectedItem.Text;

        try
        {
            myRequest = System.Net.WebRequest.Create(TexBox1.Text);
        }
        catch (Exception ex)
        {
            TexBox1.Text = "input error";
            return;
        }
        if (myRequest != null)
        {
            System.Net.WebResponse myResponse = myRequest.GetResponse();
            System.IO.Stream rssStream = myResponse.GetResponseStream();
            XmlDocument rssDoc = new System.Xml.XmlDocument();
            rssDoc.Load(rssStream);
            XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/title");
            name = rssItems.Item(0).FirstChild.Value;
            Url = TexBox1.Text;
            TexBox1.Text = name;
            Add(Url, name, Group);
        }
    }
    protected void Add(string Url, string Name,string Group)
    {
        TexBox1.Text = "";
        XmlDocument document = new XmlDocument();
        document.Load(@"D:\c#\webproject1\rss3\GhostStories.xml");

        XmlElement newRss = document.CreateElement("rss");
        XmlElement newname = document.CreateElement("name");
        XmlElement newurl = document.CreateElement("url");
        XmlText name = document.CreateTextNode(Name);
        XmlText url = document.CreateTextNode(Url);


        XmlNode menu = document.DocumentElement.FirstChild;
        XmlNode preNode = null;
        do
        {
            if (menu.Name == Group)
            {
                preNode = menu;
                break;
            }
            else
                menu = menu.NextSibling;

        }
        while (menu.NextSibling != null);
        if (preNode != null)
        {
            newRss.AppendChild(newname);
            newRss.AppendChild(newurl);
            newname.AppendChild(name);
            newurl.AppendChild(url);

            preNode.InsertBefore(newRss, preNode.FirstChild);
            document.Save(@"D:\c#\webproject1\rss3\GhostStories.xml");

            read();
        }
        else
            TexBox1.Text = "no Group";
    }
    protected XmlNode SearchGroup(XmlNode root, string Group)
    {
        XmlNode GroupNode;
       if(root==null)
           return null;
       if (root is XmlElement)
       {
           if (root.Value == Group)
               return root;
           if (root.HasChildNodes)
           {

               GroupNode =SearchGroup(root.FirstChild, Group);
               if (Group != null)
                   return GroupNode;
               else
                   return null;
           }
           if (root.NextSibling != null)
               SearchGroup(root.NextSibling, Group);

       }
        return null;
    }
}
